package com.xinqi.modules.chat.chat.chat.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import com.xinqi.common.base.response.R;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendListQueryDTO;
import com.xinqi.modules.chat.chat.friend.dto.rsp.ChatFriendInfoResultDTO;
import com.xinqi.modules.chat.chat.friend.service.ChatFriendService;
import com.xinqi.modules.chat.chat.group.dto.req.ChatGroupQueryDTO;
import com.xinqi.modules.chat.chat.group.dto.req.ConversationSelectDTO;
import com.xinqi.modules.chat.chat.group.dto.req.GroupUserFriendQueryDTO;
import com.xinqi.modules.chat.chat.group.dto.req.ShareSelectDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupInfoResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupUserInfoResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ConversationResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.GroupUserFriendResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ShareSelectResultDTO;
import com.xinqi.modules.chat.chat.group.service.ChatGroupService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WEB -- 会话列表
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.chat.chat.chat.controller.web.ConversationWebController.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/5/19 9:08
 */
@Validated
@RestController
@RequestMapping("/web/conversation")
@RequiredArgsConstructor
public class ConversationWebController {

    private final ChatGroupService chatGroupService;

    private final ChatFriendService chatFriendService;

    @Operation(summary = "搜索好友和群聊")
    @PostMapping("allConversation")
    public R<Map<String, Object>> allConversation(@Validated @RequestBody ConversationSelectDTO dto) {
        Map<String, Object> map = new HashMap<>();

        dto.setOperationUserId(LoginHelper.getUserId());
        //好友
        List<ConversationResultDTO> friend = chatFriendService.selectConversation(dto);
        //群组
        List<ConversationResultDTO> group = chatGroupService.selectConversation(dto);
        map.put("friend", friend);
        map.put("group", group);
        return R.ok(map);
    }

    @Operation(summary = "根据群聊id获取群聊信息")
    @PostMapping("/group-info")
    public R<List<ChatGroupInfoResultDTO>> groupInfo(@RequestBody ChatGroupQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        return R.data(chatGroupService.groupInfoByGroupId(query));
    }

    @Operation(summary = "根据用户id获取用户信息")
    @PostMapping("/user-info")
    public R<List<ChatFriendInfoResultDTO>> userInfo(@RequestBody ChatFriendListQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        return R.data(chatFriendService.userInfoByUserId(query));
    }

    @Operation(summary = "通过群组ID和用户ID查询群成员（在群则返回群成员身份信息）")
    @PostMapping("/group-user")
    public R<ChatGroupUserInfoResultDTO> groupUserInfo(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @RequestParam("groupId") String groupId, @NotNull(message = "user.id.not.black") @RequestParam("userId") Long userId) {
        return R.data(chatGroupService.infoByUserIdAndGroupId(userId, groupId));
    }

    @Operation(summary = "通过群组ID查看非群好友")
    @PostMapping("/group-user-friend")
    public R<List<GroupUserFriendResultDTO>> groupUserFriend(@Validated @RequestBody GroupUserFriendQueryDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(chatFriendService.groupUserFriend(dto));
    }

    @Operation(summary = "个人信息分享-不区分微信、QQ")
    @PostMapping("/share-personal")
    public R<ShareSelectResultDTO> sharePersonal(@Validated @RequestBody ShareSelectDTO dto) {
        return R.data(chatGroupService.getSharePersonalInfo(dto.getUserId()));
    }
}
